package com.lagou.edu.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 日志切面类
 * @author 10843
 */
@Component
@Aspect
public class LogUtils {


    @Pointcut("execution( * com.lagou.edu.service.impl.TransferServiceImpl.transfer(java.lang.String , java.lang.String , int))")
    public void pt1(){

    }


    /**
     * 逻辑开始前执行
     */
    @Before("pt1()")
    public void beforeMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println("业务逻辑开始执行之前执行...");
    }

    /**
     * 逻辑结束时执行
     */
    @After("pt1()")
    public void afterMethod(){
        System.out.println("业务逻辑结束前执行...,无论异常与否都执行...");
    }

    /**
     * 逻辑异常时执行
     */
    @AfterThrowing("pt1()")
    public void exceptionMethod(){
        System.out.println("异常时候执行...");
    }
    /**
     * 业务逻辑正常时执行
     */
    @AfterReturning(value = "pt1()",returning = "object")
    public void successMethod(Object object){
        System.out.println("业务逻辑正常时执行...");
    }

    /**
     * 环绕通知的方法
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("pt1()")
    public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = null;
        try{
            proceed = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        }catch(Exception e){
            System.out.println("环绕通知中的exceptionmethod....");
        }finally {
            System.out.println("环绕通知中的after method...");
        }
        return proceed;
    }
}
